package com.workshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.base.commom.ResponseUtils;
import com.workshop.dao.AArticleKeyDao;
import com.workshop.dao.ACategoryDao;
import com.workshop.dao.AUserCountDao;
import com.workshop.entity.AArticleKey;
import com.workshop.entity.ACategory;
import com.workshop.entity.AUserCount;
import com.workshop.service.IACategoryService;
import com.workshop.service.impl.base.BaseServiceImpl;
import com.workshop.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengluqiang
 * @since 2019-04-13
 */
@Service
public class ACategoryServiceImpl extends BaseServiceImpl<ACategoryDao, ACategory> implements IACategoryService {
	
	@Resource
	private ACategoryDao aCategoryDao;
	@Resource
	private AArticleKeyDao aArticleKeyDao;
	@Resource
	private AUserCountDao aUserCountDao;
	@Override
	public Map<String, Object> list(Long pageStart, Long pageTotal) {
		 //初始化分页参数
        PageUtils.initialPageStart(pageStart);
        PageUtils.initialPageTotal(pageTotal);
        long sqlStart = PageUtils.countPageStart(pageStart, pageTotal);
        List<Map<String,Object>> result = null;
        Map<String,Object> map = new HashMap<>();
        result = aCategoryDao.getCategoryList(sqlStart,pageTotal);
        long pageCount = aCategoryDao.getCategoryCount();
        map.put("pageStart",pageStart);
        map.put("pageTotal",pageTotal);
        map.put("pageCount",pageCount);
        map.put("data",result);
        return ResponseUtils.success(map);
	}
	@Override
	public Map<String, Object> setCategory(String json) {
		if(json==null||StringUtils.isBlank(json)) {
			return ResponseUtils.error("数据为空！");
		}
		Map<String,Object> map = JSON.parseObject(json);
		if(map==null||map.isEmpty()
				||map.get("artId")==null||StringUtils.isBlank(map.get("artId").toString())
						||map.get("categroyIds")==null||StringUtils.isBlank(map.get("categroyIds").toString())) {
			return ResponseUtils.error("数据为空！");
		}
		
		List<Long> catIds = JSON.parseArray(map.get("categroyIds").toString(),Long.class);
		
		AArticleKey aArticleKey = new AArticleKey();
		aArticleKey.setArticleId(map.get("artId").toString());
		EntityWrapper<AArticleKey> entityWrapper = new EntityWrapper<>(aArticleKey);
		aArticleKey.delete(entityWrapper);
		for(Long catId : catIds) {
			aArticleKey.setCategoryId(catId);
			aArticleKey.insert();
		}
		return ResponseUtils.success("设置分类成功！");
	}

	@Override
	public Map<String, Object> add(String name) {
		if(name==null||StringUtils.isBlank(name)) {
			return ResponseUtils.error("数据为空！");
		}
		ACategory category = new ACategory();
		category.setName(name);
		List list = aCategoryDao.selectList(new EntityWrapper(category));
		if(list!=null&&!list.isEmpty()){
			return ResponseUtils.error("该分类已经存在!");
		}
		category.setParentId(0);
		category.setDepth(1);
		category.setParse("-1-");
		category.setCreateTime(new Date());
		category.setUpdateTime(new Date());
		aCategoryDao.insert(category);
		return ResponseUtils.success("新增分类成功！");
	}

	@Override
	public Map<String, Object> del(String idJson) {
		if(idJson==null||StringUtils.isBlank(idJson)){
			return ResponseUtils.error("json参数错误!");
		}
		List<String> ids = JSON.parseArray(idJson,String.class);
		if(ids==null||ids.isEmpty()){
			return ResponseUtils.error("json参数错误!");
		}
		for(String id :ids){
			aCategoryDao.delete(new EntityWrapper<>(new ACategory().setId(Long.parseLong(id))));
			aArticleKeyDao.delete(new EntityWrapper<>(new AArticleKey().setCategoryId(Long.parseLong(id))));
			aUserCountDao.delete(new EntityWrapper<>(new AUserCount().setCategoryId(Long.parseLong(id))));
		}
		return ResponseUtils.success("删除分类成功！");
	}

}
